package indi.zn.commons.mail;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.google.common.base.Strings;


public class MailPropertiesFactory {
	
	private static  Properties global_props;
	
	public synchronized static Properties getMailPropertiesFromFile(String propertiesClassPath) throws IOException{
		
		return getMailPropertiesFromFile( propertiesClassPath,false);
	}
	
	
	/**
	 * 
	 * @param propertiesClassPath  注意从根路径开始  （开始加 / 和不加 / 的情况）
	 * @param global
	 * @return
	 * @throws IOException
	 */
	public synchronized static Properties getMailPropertiesFromFile(String propertiesClassPath,boolean global) throws IOException{
		
		Properties props =null; 
		
		if(!global){
			props=new Properties();		
			
			InputStream is= MailPropertiesFactory.class.getClassLoader().getResourceAsStream(propertiesClassPath);
			props.load(is);
		}
		else if(global&&global_props==null){
			
			props=new Properties();		
			
			InputStream is= MailPropertiesFactory.class.getClassLoader().getResourceAsStream(propertiesClassPath);
			props.load(is);
			global_props=props;
			
		}else if(global&&global_props!=null){
		
			props=global_props;
			
		}
		
		return props;
	}
	
	
	public  static Properties  getMailPropertiesFromData(String protocol,String host,Integer port,String from,String clazz,String user,Boolean debug){
		
		Properties props = new Properties();
		
		if(!Strings.isNullOrEmpty(host))   props.put("mail."+protocol+".host", host);
		if(null!=port)   props.put("mail."+protocol+".port", port);
		if(!Strings.isNullOrEmpty(user))   props.put("mail."+protocol+".user", user);
		if(!Strings.isNullOrEmpty(clazz))   props.put("mail."+protocol+".class", clazz);
		if(!Strings.isNullOrEmpty(host))   props.put("mail.from", from);
		if(null!=debug)  props.put("mail.debug", debug);
		  
		return props;
	}
	
  public  static Properties  getMailPropertiesFromData(String protocol,String host,int port,String from){
		
		return getMailPropertiesFromData(protocol, host, port, from, null, null, null);
	}
	

}
